package net.zjitc.demo1.service.Impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.zjitc.demo1.dao.UserDao;
import net.zjitc.demo1.pojo.ResultData;
import net.zjitc.demo1.pojo.User;
import net.zjitc.demo1.service.UserService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;


import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public Boolean add(User user) {
        return userDao.insert(user) == 1;
    }

    @Override
    public Boolean deleteById(Integer id) {
        return userDao.deleteById(id) == 1;
    }

    @Override
    public Boolean update(User user) {
        return userDao.updateById(user) == 1;
    }

    @Override
    public ResultData<User> findById(Integer id) {
        List<User> a=new ArrayList<>();
        a.add(userDao.selectById(id));
        return new ResultData<User>((long)1,a);
    }


    @Override
    public ResultData<User> findPage(int currentPage, int pageSize, User user) {
        IPage page = new Page(currentPage, pageSize);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Strings.isNotEmpty(user.getUsername()), User::getUsername, user.getUsername());
        wrapper.like(Strings.isNotEmpty(user.getGender()), User::getGender, user.getGender());
        wrapper.like(Strings.isNotEmpty(user.getEmail()), User::getEmail, user.getEmail());
        userDao.selectPage(page, wrapper);
        //返回两个值，一个是list形式的查询结果，另外一个是总数total
        return new ResultData<User>(page.getTotal(),page.getRecords());
    }


}
